import { Message } from 'element-ui'
import store from '@/store'
import axios from 'axios'
const request = axios.create({
  // baseURL: process.env.VUE_APP_BASE_URL,
  baseURL: 'http://likede2-admin.itheima.net/likede/',
  timeout: 20000 // request timeout
})

// 请求拦截器
request.interceptors.request.use(
  config => {
    if (!config.headers.Authorization && store.state.user.token) {
      config.headers.Authorization = `${store.state.user.token}`
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器
request.interceptors.response.use(
  response => {
    const { msg, success, status } = response.data
    // console.log(response)
    if (success || status || response.status === 200) {
      return response.data
    } else {
      if (msg) {
        Message.error(msg)
        return Promise.reject(msg)
      }
    }
    return Promise.reject()
  },
  error => {
    return Promise.reject(error)
  }
)

export default request
